<template>
  <div class="app-container">
    <div class="filter-container">
      <SearchOptionClass :class-id="listQuery.class_id" @setSearchClassId="searchClassId" />
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
        查询
      </el-button>
      <el-button class="filter-item" style="margin-left: 10px;" type="primary" icon="el-icon-edit" @click="handleCreate">
        添加
      </el-button>
    </div>

    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :header-cell-style="{background:'#ccc',color:'black'}"
      :data="list"
      border
      stripe
      fit
      highlight-current-row
      style="width: 100%;"
    >
      <el-table-column label="ID" align="center" width="80" prop="id">
        <template slot-scope="{row}">
          <span>{{ row.id }}</span>
        </template>
      </el-table-column>
      <el-table-column label="班级名称" align="center">
        <template slot-scope="{row}">
          <span>{{ row.classInfo.name }}</span>
        </template>
      </el-table-column>
      <el-table-column label="信用学分" align="center">
        <template slot-scope="{row}">
          <span>{{ row.credit_score }}</span>
        </template>
      </el-table-column>
      <el-table-column label="应修学分" align="center">
        <template slot-scope="{row}">
          <span>{{ row.should_score }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="230" class-name="small-padding fixed-width">
        <template slot-scope="{row,$index}">
          <el-button type="primary" size="mini" @click="handleUpdate(row)">
            编辑
          </el-button>
          <el-button type="danger" size="mini" @click="handleDelete(row,$index)">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />

    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
      <el-form ref="dataForm" :rules="rules" :model="temp" label-position="left" label-width="110px" style="width: 400px; margin-left:50px;">
        <el-form-item v-if="textMap[dialogStatus] === '添加'" label="班级名称" prop="class_id">
          <SearchOptionClass :class-id="listQuery.class_id" style="width:290px" @setSearchClassId="searchUpdateClassId" />
        </el-form-item>
        <el-form-item v-else label="班级名称" prop="class_id">
          <el-input :value="`${temp.class_id}|${className}`" disabled />
        </el-form-item>
        <el-form-item label="信用学分" prop="credit_score">
          <el-input-number v-model="temp.credit_score" />
        </el-form-item>
        <el-form-item label="应修学分" prop="should_score">
          <el-input-number v-model="temp.should_score" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">
          取消
        </el-button>
        <el-button type="primary" @click="dialogStatus==='create'?createData():updateData()">
          确定
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { scoreClassPage, scoreClassUpdate, scoreClassDelete } from '@/api/xuanxiu'
import waves from '@/directive/waves' // waves directive

import { notifySuccess, my_confirm, listQueryFilter } from '@/utils/tool'
import SearchOptionClass from '@/components/SearchOption/class'
import Pagination from '@/components/Pagination'

export default {
  name: 'ScoreClass',
  components: { Pagination, SearchOptionClass },
  directives: { waves },
  data() {
    return {
      tableKey: 0,
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 20,
        class_id: null
      },
      listQueryClass: {
        page: 1,
        limit: 999,
        querySort: '-id'
      },
      temp: {
        id: null,
        name: null,
        address: null,
        sort: ''
      },
      dialogFormVisible: false,
      dialogStatus: '',
      textMap: {
        update: '编辑',
        create: '添加'
      },
      rules: {
        class_id: [{ required: true, message: '班级名称必填', trigger: 'change' }],
        credit_score: [{ required: true, message: '信用学分必填', trigger: 'change' }],
        should_score: [{ required: true, message: '应修学分必填', trigger: 'change' }]
      },
      downloadLoading: false,
      options: [],
      className: ''
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      this.listQuery = listQueryFilter(this.listQuery)
      scoreClassPage(this.listQuery).then(response => {
        this.list = response.data
        this.total = response.page
        this.listLoading = false
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    resetTemp() {
      this.temp = {
        class_id: null,
        credit_score: null,
        should_score: null
      }
    },
    handleCreate() {
      this.resetTemp()
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    createData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          scoreClassUpdate(this.temp).then(response => {
            var rs = response.data
            this.list.unshift(rs)
            this.dialogFormVisible = false
            notifySuccess(this, '创建成功')
            this.getList()
          })
        }
      })
    },
    handleUpdate(row) {
      this.temp = Object.assign({}, row)
      this.className = row.classInfo.name
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    updateData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          delete this.temp.classInfo
          const tempData = Object.assign({}, this.temp)
          scoreClassUpdate(tempData).then(response => {
            var rs = response.data
            const index = this.list.findIndex(v => v.id === rs.id)
            this.list.splice(index, 1, rs)
            this.dialogFormVisible = false
            notifySuccess(this, '修改成功')
            this.getList()
          })
        }
      })
    },
    handleDelete(row, index) {
      my_confirm(this, () => {
        scoreClassDelete({ id: row.id }).then(() => {
          this.list.splice(index, 1)
          this.dialogFormVisible = false
          notifySuccess(this, '删除成功')
        })
      })
    },
    getSortClass: function(key) {
      const sort = this.listQuery.querySort
      return sort === `+${key}` ? 'ascending' : 'descending'
    },
    searchClassId: function(data) {
      this.listQuery.class_id = data.classId
    },
    searchUpdateClassId: function(data) {
      this.temp.class_id = data.classId
    }
  }
}
</script>
